package com.zmh.license.info;


import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-03-13 11:06
 */
@Slf4j
public class WindowsServer extends LicenseAbstractServer {

    @Override
    protected List<String> getIpAddress() {
        List<InetAddress> localAllInetAddress = getLocalAllInetAddress();
        return localAllInetAddress.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList());
    }

    @Override
    protected List<String> getMacAddress() {
        List<InetAddress> localAllInetAddress = getLocalAllInetAddress();
        return localAllInetAddress.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
    }

    @Override
    protected String getCpuInfo() throws IOException {
        return common("wmic cpu get processorid");
    }

    private String common(String command) throws IOException {
        String serialNumber = "";
        Process process = Runtime.getRuntime().exec(command);
        process.getOutputStream().close();
        Scanner scanner = new Scanner(process.getInputStream());

        if(scanner.hasNext()){
            scanner.next();
        }

        if(scanner.hasNext()){
            serialNumber = scanner.next().trim();
        }

        scanner.close();
        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws IOException {
        return common("wmic baseboard get serialnumber");
    }
}
